package com.example.server.impl;

import com.example.dao.deptMapper.DeptMapper;
import com.example.dao.jobMapper.JobMapper;
import com.example.dao.roleMapper.RoleMapper;
import com.example.dao.userMapper.UserMapper;
import com.example.dto.dept.Dept;
import com.example.dto.job.Job;
import com.example.dto.role.Role;
import com.example.dto.user.User;
import com.example.dto.user.UserCriteria;
import com.example.exception.EntityExistException;
import com.example.server.UserServer;
import com.example.util.DateUtil;
import com.example.util.ValidationUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDateTime;
import java.util.*;

@Service
public class UserServerImpl implements UserServer {

    @Autowired
    private UserMapper userMapper;
    @Autowired
    private DeptMapper deptMapper;
    @Autowired
    private RoleMapper roleMapper;
    @Autowired
    private JobMapper jobMapper;
    /**
     * 查找用户
     *
     * @param userCriteria
     * @return Map<String, Object>
     */
    @Override
    public Map<String, Object> list(UserCriteria userCriteria) {
        Map<String, Object> result = new HashMap<>(4);

        Map<String, Object> hash = new HashMap<>();
        hash.put("createTime",null);
        hash.put("updateTime", null);
        hash.put("blurry", userCriteria.getBlurry());
        hash.put("enabled", userCriteria.getEnabled());
        hash.put("deptId", userCriteria.getDeptId());
        hash.put("deptIds",userCriteria.getDeptIds());
        hash.put("page", userCriteria.getPage());
        hash.put("size", userCriteria.getSize());
        List<User> users = userMapper.selectList(hash);
        int count = userMapper.findCount(hash);
        users.forEach(x -> {
            Long deptId = x.getDeptId();
            Dept dept = deptMapper.findByID(deptId);
            x.setDept(dept);
            List<Role> role = roleMapper.findByUserIds(x.getId());
            x.setRoles(role);
            List<Job> job = jobMapper.findByID(x.getId());
            x.setJobs(job);
            x.setLabel(x.getUsername());
        });
        result.put("content", users);
        result.put("totalElements", count);
        return result;
    }

    /**
     * 新增用户
     *
     * @param user
     * @return Map<String, Object>
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public void create(User resources) {
        if (userMapper.findByUsername(resources.getUsername()) != null) {
            throw new EntityExistException(User.class, "username", resources.getUsername());
        }
        if (userMapper.findByEmail(resources.getEmail()) != null) {
            throw new EntityExistException(User.class, "email", resources.getEmail());
        }
        if (userMapper.findByPhone(resources.getPhone()) != null) {
            throw new EntityExistException(User.class, "phone", resources.getPhone());
        }
        userMapper.creat(resources);
        User user = userMapper.findByUsername(resources.getUsername());
        userMapper.creatRole(user,resources.getRoles());
        userMapper.creatJobs(user,resources.getJobs());
    }

    /**
     * 删除用户
     *
     * @param
     * @return Map<String, Object>
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public void delete( Set<Long> ids) {
        userMapper.deleRoles(ids);
        userMapper.deleJobs(ids);
        userMapper.deleteById(ids);
    }

    /**
     * 修改用户
     *
     * @param user
     * @return Map<String, Object>
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public void update(User resources) throws Exception {
        User user = userMapper.findById(resources.getId());
        ValidationUtil.isNull(user.getId(), "User", "id", resources.getId());
        User user1 = userMapper.findByUsername(resources.getUsername());
        User user2 = userMapper.findByEmail(resources.getEmail());
        User user3 = userMapper.findByPhone(resources.getPhone());
        if (user1 != null && !user.getId().equals(user1.getId())) {
            throw new EntityExistException(User.class, "username", resources.getUsername());
        }
        if (user2 != null && !user.getId().equals(user2.getId())) {
            throw new EntityExistException(User.class, "email", resources.getEmail());
        }
        if (user3 != null && !user.getId().equals(user3.getId())) {
            throw new EntityExistException(User.class, "phone", resources.getPhone());
        }

        roleMapper.delRole(resources);
        roleMapper.addRole(resources, resources.getRoles());
        jobMapper.delJob(resources);
        jobMapper.addJob(resources,resources.getJobs());

//        user.setUsername(resources.getUsername());
//        user.setEmail(resources.getEmail());
//        user.setEnabled(resources.getEnabled());
//        user.setRoles(resources.getRoles());
//        user.setDept(resources.getDept());
//        user.setJobs(resources.getJobs());
//        user.setPhone(resources.getPhone());
//        user.setNickName(resources.getNickName());
//        user.setGender(resources.getGender());
        userMapper.save(user);
    }

}
